const tagsView = defineStore("tagsView",{
	state: ()=>({
	    visitedViews: [],
	    cachedViews: [],
	    iframeViews: []
	}),
	actions:{
		addView(view) {
			this.addVisitedView(view)
			this.addVisitedView(view)
		},
		addIframeView(view) {
			this.ADD_IFRAME_VIEW(view)
		},
		addVisitedView(view) {
			this.ADD_VISITED_VIEW(view)
		},
		addCachedView(view) {
			this.ADD_CACHED_VIEW(view)
		},
		delView(view) {
			return new Promise(resolve => {
				this.delVisitedView(view)
				this.delCachedView(view)
				
				resolve({
					visitedViews: [...this.visitedViews],
					cachedViews: [...this.cachedViews]
				})
			})
		},
		delVisitedView(view) {
			return new Promise(resolve => {
				this.DEL_VISITED_VIEW(view)
				resolve([...this.visitedViews])
			})
		},
		delIframeView(view) {
			return new Promise(resolve => {
				this.DEL_IFRAME_VIEW(view)
				resolve([...this.iframeViews])
			})
		},
		delCachedView(view) {
			return new Promise(resolve => {
				this.DEL_CACHED_VIEW(view)
				resolve([...this.cachedViews])
			})
		},
		delOthersViews(view) {
			return new Promise(resolve => {
				this.delOthersVisitedViews(view)
				this.delOthersCachedViews(view)
				resolve({
					visitedViews: [...this.visitedViews],
					cachedViews: [...this.cachedViews]
				})
			})
		},
		delOthersVisitedViews(view) {
			return new Promise(resolve => {
				this.DEL_OTHERS_VISITED_VIEWS(view)
				resolve([...this.visitedViews])
			})
		},
		delOthersCachedViews(view) {
			return new Promise(resolve => {
				this.DEL_OTHERS_CACHED_VIEWS(view)
				resolve([...this.cachedViews])
			})
		},
		delAllViews(view) {
			return new Promise(resolve => {
				this.delAllVisitedViews(view)
				this.delAllCachedViews(view)
				resolve({
					visitedViews: [...this.visitedViews],
					cachedViews: [...this.cachedViews]
				})
			})
		},
		delAllVisitedViews() {
			return new Promise(resolve => {
				this.DEL_ALL_VISITED_VIEWS()
				resolve([...this.visitedViews])
			})
		},
		delAllCachedViews() {
			return new Promise(resolve => {
				this.DEL_ALL_CACHED_VIEWS()
				resolve([...this.cachedViews])
			})
		},
		updateVisitedView(view) {
			this.UPDATE_VISITED_VIEW(view)
		},
		delRightTags(view) {
			return new Promise(resolve => {
				this.DEL_RIGHT_VIEWS(view)
				resolve([...this.visitedViews])
			})
		},
		delLeftTags(view) {
			return new Promise(resolve => {
				this.DEL_LEFT_VIEWS(view)
				resolve([...this.visitedViews])
			})
		},
		
		ADD_IFRAME_VIEW(view){
			if (this.iframeViews.some(v => v.path === view.path)) return
			this.iframeViews.push(
				Object.assign({}, view, {
					title: view.meta.title || 'no-name'
				})
			)
		},
		ADD_VISITED_VIEW(view) {
			if (this.visitedViews.some(v => v.path === view.path)) return
			this.visitedViews.push(
				Object.assign({}, view, {
					title: view.meta.title || 'no-name'
				})
			)
		},
		ADD_CACHED_VIEW(view){
			if (this.cachedViews.includes(view.name)) return
			if (view.meta && !view.meta.noCache) {
				this.cachedViews.push(view.name)
			}
		},
		DEL_VISITED_VIEW(view){
			for (const [i, v] of this.visitedViews.entries()) {
				if (v.path === view.path) {
					this.visitedViews.splice(i, 1)
					break
				}
			}
			this.iframeViews = this.iframeViews.filter(item => item.path !== view.path)
		},
		DEL_IFRAME_VIEW(view){
			this.iframeViews = this.iframeViews.filter(item => item.path !== view.path)
		},
		DEL_CACHED_VIEW(view){
			const index = this.cachedViews.indexOf(view.name)
			index > -1 && this.cachedViews.splice(index, 1)
		},
		
		DEL_OTHERS_VISITED_VIEWS(view){
			this.visitedViews = this.visitedViews.filter(v => {
				return v.meta.affix || v.path === view.path
			})
			this.iframeViews = this.iframeViews.filter(item => item.path === view.path)
		},
		DEL_OTHERS_CACHED_VIEWS(view){
			const index = this.cachedViews.indexOf(view.name)
			if (index > -1) {
				this.cachedViews = this.cachedViews.slice(index, index + 1)
			} else {
				this.cachedViews = []
			}
		},
		DEL_ALL_VISITED_VIEWS(){
			// keep affix tags
			const affixTags = this.visitedViews.filter(tag => tag.meta.affix)
			this.visitedViews = affixTags
			this.iframeViews = []
		},
		DEL_ALL_CACHED_VIEWS(){
			this.cachedViews = []
		},
		UPDATE_VISITED_VIEW(view){
			for (let v of this.visitedViews) {
				if (v.path === view.path) {
					v = Object.assign(v, view)
					break
				}
			}
		},
		DEL_RIGHT_VIEWS(view) {
			const index = this.visitedViews.findIndex(v => v.path === view.path)
			if (index === -1) {
				return
			}
			this.visitedViews = this.visitedViews.filter((item, idx) => {
				if (idx <= index || (item.meta && item.meta.affix)) {
					return true
				}
				const i = this.cachedViews.indexOf(item.name)
				if (i > -1) {
					this.cachedViews.splice(i, 1)
				}
				if (item.meta.link) {
					const fi = this.iframeViews.findIndex(v => v.path === item.path)
					this.iframeViews.splice(fi, 1)
				}
				return false
			})
		},
		DEL_LEFT_VIEWS(view) {
			const index = this.visitedViews.findIndex(v => v.path === view.path)
			if (index === -1) {
				return
			}
			this.visitedViews = this.visitedViews.filter((item, idx) => {
				if (idx >= index || (item.meta && item.meta.affix)) {
					return true
				}
				const i = this.cachedViews.indexOf(item.name)
				if (i > -1) {
					this.cachedViews.splice(i, 1)
				}
				if (item.meta.link) {
					const fi = this.iframeViews.findIndex(v => v.path === item.path)
					this.iframeViews.splice(fi, 1)
				}
				return false
			})
		}
	}
})

export default tagsView